﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using nsBaseClass;
using System.Configuration;
using MLVS.ServiceReference1;
using MLVS.Model;
using MLVS.AppUtils.Settings;
using MLVS.Database;
using MLVS.AppUtils;

namespace MLVS
{
    class Program
    {
        private static readonly ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();     //Configuring log
            //connect DB
            var objGlobal = new clsGlobalVariable();                                    //clsGlovalVariable for connecting to SQL DB
            objGlobal.InitFromCommandLine(ConfigurationSettings.AppSettings["DBName"],  //Get login data from app.config
                                          ConfigurationSettings.AppSettings["WinAut"],
                                          ConfigurationSettings.AppSettings["DBUser"],
                                          ConfigurationSettings.AppSettings["DBPass"],
                                          ConfigurationSettings.AppSettings["Site"]);
            List<CVINS> cvins = new List<CVINS>();
            string id = AppConfig.GetSettingValue("RETAILERID", "");
            int retailerID = -1;
            int.TryParse(id, out retailerID);

            cvins = GetVincesList(retailerID);

           

            if (cvins != null && cvins.Count > 0)
            {
                // Delete table
                DBAccess.TruncateCVOEQ();
                DBAccess.TruncateCVINS();
                //insert to table
                DBAccess.insertCVINS(cvins);
            }
            else
                logger.Debug("Nothing to insert...");


            logger.Debug("MLVS done !");
           // Console.ReadKey();
        }

        public static List<CVINS> GetVincesList(int retailerID)
        {
            try
            {

                // call services
                var client = new ServicesSoapClient();
                VehicleDTO[] vehicles = new VehicleDTO[] { };

                vehicles = client.GetVehicles(retailerID);
                List<CVINS> cvins = new List<CVINS>();
                List<CVOEQ> cvoeqs = new List<CVOEQ>();

                if (vehicles != null && vehicles.Length > 0)
                {
                    foreach (var v in vehicles)
                    {
                        CVINS t = new CVINS();
                        t.ASKPR = StringUtils.toStringFormat(v.Price);
                        t.COLOURCD = v.ExtColour.Code;
                        t.DEFVATCD = AppConfig.GetSettingValue("DEFVATCD", "");
                        t.ESVATCD = AppConfig.GetSettingValue("ESVATCD", "");
                        t.EWA = v.ETA.ToString();
                        t.IMPCODE = v.URN;
                        t.ISPUBLISHED = AppConfig.GetSettingValue("ISPUBLISHED", "0");
                        t.LICNO = v.URN;
                        t.LOCATION = v.Location;
                        t.MODEL = v.Model.ModelFamily.Name;
                        t.LISTPRICE = StringUtils.toStringFormat(v.Model.NormalPrice);
                        t.MAKE = AppConfig.GetSettingValue("MAKE", "Toyota");
                        t.MLVSDEALER = AppConfig.GetSettingValue("MLVSDEALER", "");
                        t.NOTE = "";
                        t.SERIALNO = v.URN;
                        t.SMANID = AppConfig.GetSettingValue("SMANID", "");
                        t.SUBMODEL = v.Model.Name;
                        t.TOTOEQS = "0"; // ?????
                        t.TOTOEQS0 = "0"; // ?????
                        t.TRIMCD = v.IntColour;
                        t.TYPECODE = v.Model.Code;
                        t.VISIBLE = AppConfig.GetSettingValue("VISIBLE", "0");
                        t.UNITID = AppConfig.GetSettingValue("UNITID", "");

                        if (v.FactoryOptions != null && v.FactoryOptions.Length > 0)
                        {
                            foreach (var tem in v.FactoryOptions)
                            {
                                CVOEQ t2 = new CVOEQ();
                                t2.ACCCODE = tem.Code;
                                t2.Name = tem.Name;
                                t2.SELPR = tem.Price;
                                cvoeqs.Add(t2);
                            }
                        }
                        t.cvoeqs = cvoeqs;
                        //apply VAT
                        t = VATHandling(t);
                        cvins.Add(t);

                    }
                }
                return cvins;
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                return null;
            }
        }


        public static CVINS VATHandling(CVINS cvin)
        {
            string Accuracy = AppConfig.GetSettingValue("ACCURACY", "6");
            string VATCode = AppConfig.GetSettingValue("VATCODE", "2");
            int nAccuracy = 6;

            int.TryParse(Accuracy, out nAccuracy);
            clsTaxHandling taxHandling = new clsTaxHandling();
            taxHandling.Init(nAccuracy);
            // SELPR is value with VAT
            List<CVOEQ> cvoeqs = cvin.cvoeqs;
            decimal? TOTOEQS = 0;
            if (cvoeqs != null && cvoeqs.Count > 0)
            {
                foreach (var v in cvoeqs)
                {
                    TOTOEQS += v.SELPR;
                }
            }
            decimal? TOTOEQS0 = 0;
            TOTOEQS0 = taxHandling.Remove(TOTOEQS, VATCode, DateTime.Now);

            cvin.TOTOEQS = TOTOEQS == null ? "0" : StringUtils.toStringFormat((decimal)TOTOEQS);
            cvin.TOTOEQS0 = TOTOEQS0 == null ? "0" : StringUtils.toStringFormat((decimal)TOTOEQS0);

            return cvin;


        }














    }
}
